/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

// 哈希表
var hasCycle = function (head) {
  let set = new Set()
  for (let p = head; p; p = p.next) {
    if (set.has(p)) return true
    set.add(p)
  }
  return false
};

// 快慢指针
var hasCycle = function (head) {
  if (head) {
    let slow = head
    let fast = head.next
    for (; fast;) {
      if (fast === slow) return true
      slow = slow.next
      if (!fast.next) return false
      fast = fast.next.next
    }
  }
  return false
}

